﻿package pingce.share.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;

import pingce.share.model.raw.TestDocument;
import pingce.share.model.raw.Weibo;
import pingce.share.util.SentenceProcessor;

/**
 * 对应于一个话题（原始数据存放在一个xml文档中）的处理（分子句->分词->句法分析）结果。
 * 处理之后，序列化到本地的一个文件当中，便于下次调用。
 * @author ucai
 */
public class TestDocumentResult implements Serializable {
	
	private static final long serialVersionUID = -6691434583426257655L;
	/**
	 * weiboResultList顺序存放每条微博的处理结果；
	 * weiboResultMap以微博的id为key存放结果。
	 */
	private LinkedList<WeiboResult> weiboResultList;
	private HashMap<Integer, WeiboResult> weiboResultMap;
	public TestDocumentResult(TestDocument td, SentenceProcessor sp){
		this.weiboResultList = new LinkedList<WeiboResult>();
		this.weiboResultMap = new HashMap<Integer, WeiboResult>();
		for(Weibo w : td.weibos){
			System.out.println("processing " + w.id + "....");
			WeiboResult wr = new WeiboResult(w, sp);
			this.weiboResultList.add(wr);
			this.weiboResultMap.put(w.id, wr);
		}
	}
	public TestDocumentResult(TestDocument td){
		this(td, new SentenceProcessor());
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for(WeiboResult wr : this.weiboResultList)
			sb.append(wr.toString() + "\n");
		return sb.toString();
	}
	public LinkedList<WeiboResult> getWeiboResultList() {
		return weiboResultList;
	}

	public void setWeiboResultList(LinkedList<WeiboResult> weiboResultList) {
		this.weiboResultList = weiboResultList;
	}

	public HashMap<Integer, WeiboResult> getWeiboResultMap() {
		return weiboResultMap;
	}

	public void setWeiboResultMap(HashMap<Integer, WeiboResult> weiboResultMap) {
		this.weiboResultMap = weiboResultMap;
	}

	/**
	 * @param modelPath 序列化到本地的文件路径
	 */
	public void writeToFile(String modelPath){
		try{
			ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream
					(modelPath));
			oo.writeObject(this);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @param modelPath 从本地文件中反序列化该对象
	 * @return
	 */
	public static TestDocumentResult readForm(String modelPath){
		TestDocumentResult tdr = null;
		try{
			ObjectInputStream in = new ObjectInputStream(new FileInputStream
					(modelPath));
			tdr = (TestDocumentResult)in.readObject();
		}catch(Exception e){
			e.printStackTrace();
		}
		return tdr;
	}
	
	/**
	 * 
	 * @param filePath 将结果打印到该路径指示的文本中，便于观察结果。
	 * @return
	 */
	public String printToFile(String filePath){
		try{
			PrintWriter pw = new PrintWriter(new File(filePath));
			pw.println(this.toString());
			pw.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return this.toString();
	}
	
	public static TestDocumentResult getInstance(String topicName)
	{
		File tmp = new File("");
		return readForm(tmp.getAbsolutePath()+"/model/"+topicName+".model");
	}

	public static void main(String[] args){
		String xmlDir = "C:/Users/ZZY/Desktop/评测/微博情感-样例数据/labelled test dataset";
		String fileName = "feng_kuang_de_da_cong.xml";
//		String fileName = "hui_rong_an（no hash and blank）.xml";
//		String fileName = "ipad.xml";
//		String fileName = "ipad(no hash).xml";
//		String fileName = "hui_rong_an.xml";
//		String fileName = "che_huo_100test.xml";
//		String fileName = "标注(301-600).xml";
//		String fileName = "my_job_401(done).xml";
//		String fileName = "an_hui_xiao_xian_che_huo(1-1001).xml";
		String filePath = xmlDir + "/" + fileName;
		String modelPath = filePath + ".TestDocumentResult.model";
		
		TestDocumentResult tdr = new TestDocumentResult(TestDocument.getInstance(filePath));
		tdr.writeToFile(modelPath);
		
		System.out.println(tdr.printToFile(filePath + ".TestDocumentResult.result"));
		
	}
}
